<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/5/15
 * Time: 15:26
 */

namespace App\Rep;


use App\Library\PrintingOptions\PrintingOptions;
use App\Models\Invoices;
use App\Models\OrderPrint;

class OrderPrintRep
{
    /**
     * 工单打印列表
     * @param $params
     */
    public function getOrderPrintListRep($params){
        $orderPrintList = OrderPrint::Search($params)->with(['order_child'=>function($q){
            $q->with(['order'=>function($q){
                return $q->with(['customer','modelNumber','color','specification','product']);
            },'employee','job_type']);
        }])->orderBy('id','desc')->get()->toArray();

        $arr = [];
        $invoice = Invoices::where('is_show',1)->orderBy('sort','asc')->get();
        $pritingOptions = new PrintingOptions();
        foreach($orderPrintList as $key=>$val){
            //p($val['order_child']);
            $arr[$key] = $pritingOptions->getOrderInvoices($invoice,$val['order_child']);
            $arr[$key]['order_child_id'] = $val['order_child']['id']??'';
            $arr[$key]['order_print_id'] = $val['id']??'';
        }
        return $arr;
    }

    /**
     * 添加打印
     * @param $params
     */
    public function addOrderPrintRep($params){
        $order_child_ids = $params['order_child_ids'];
        foreach($order_child_ids as $k=>$v){
            OrderPrint::updateOrCreate(
                ['order_id'=>$params['order_id'],'order_child_id'=>$v],
                ['order_id'=>$params['order_id'],'order_child_id'=>$v,'is_print'=>2,'is_del'=>2]
            );
        }
        return true;
    }


    /**
     * 删除打印
     * @param $params
     */
    public function delOrderPrintRep($params){
        if(is_array($params['order_print_id'])){
            return OrderPrint::whereIn('id',$params['order_print_id'])->update(['is_del'=>1]);
        }else{
            return OrderPrint::where('id',$params['order_print_id'])->update(['is_del'=>1]);
        }

    }

}